/*
 * @Author: OreoWang
 * @Email: ihc523@163.com
 * @Date: 2022-08-08 09:41:52
 * @LastEditors: OreoWang
 * @LastEditTime: 2022-08-17 15:35:54
 * @Description: 
 */
import { Component } from "cc";
import { EDITOR } from "cc/env"
import { TypeCompiler } from "./ViewCompilerType";
import CompileCCEditBox from "./CompileCCEditBox";
import CompileCCLabel from "./CompileCCLabel";
import CompileCCRichText from "./CompileCCRichText";
import CompileCCToggle from "./CompileCCToggle";
import { isSupportRuntime } from "../ViewTool";

class ViewCompiler {
    private inited = false;
    private listCompilers: TypeCompiler[] = [];

    constructor(){
        if(!isSupportRuntime()){
            return;
        }
        this.init();
    }

    add(compiler: TypeCompiler){
        let item = this.listCompilers.find((item)=>{
            return item.component == compiler.component;
        })
        if(item){
            console.error("Exist compiler: ", item);
            return;
        }

        this.listCompilers.push(compiler);
    }

    init(){
        if(this.inited){
            return;
        }
        
        this.inited = true;        
        this.add(CompileCCLabel());
        this.add(CompileCCEditBox());
        this.add(CompileCCRichText());
        this.add(CompileCCToggle());
    }

    bindElement(vm: any, attr: string, component: Component, hasSetter: boolean): any {
        let listCompilers = this.listCompilers;
        for (let index = 0; index < listCompilers.length; index++) {
            const element = listCompilers[index];
            if(component instanceof element.component){
                return element.binder(vm, attr, component, hasSetter);
            }
        }
    }
}

const viewCompiler = new ViewCompiler();
export { viewCompiler };